package com.uzai.console.web.qiniu;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.dto.qiniu.QiniuConfigDto;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.qiniu.QiniuService;
import com.uzai.console.vo.qiniu.QiniuConfigInfoVo;
import com.uzai.console.vo.qiniu.QiniuConfigUploadFileVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 七牛云
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "七牛云",tags = "系统设置|七牛云")
@ApiSort(29)
@RestController
public class QiniuController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(QiniuController.class);

    @Autowired
    private QiniuService qiniuService;
    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;


    @ApiOperation(value = "验证七牛云配置", notes = "验证七牛云配置")
    @ApiOperationSupport(order = 1)
    @PostMapping("/qiniu/checkConfig")
    public Object checkConfig(@RequestBody QiniuConfigDto qiniuConfigDto) throws Exception {
        Boolean ckeckFlag = qiniuService.checkConfig(qiniuConfigDto);
        if(ckeckFlag){
            throw new BusinessException(ResponseCode.SUCCESS, "验证正确");
        }else{
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "配置错误");
        }
    }

    @ApiOperation(value = "保存七牛云配置", notes = "保存七牛云配置")
    @ApiOperationSupport(order = 2)
    @PostMapping("/qiniu/addQiniuConfig")
    public Object addQiniuConfig(@RequestBody QiniuConfigDto qiniuConfigDto){
        qiniuService.addQiniuConfig(qiniuConfigDto);
        return true;
    }

    @ApiOperation(value = "上传本地图片", notes = "上传本地图片", response = QiniuConfigUploadFileVo.class)
    @ApiOperationSupport(order = 3)
    @PostMapping("/qiniu/uploadImage")
    public Object uploadImage(MultipartFile file){
        Long merId = getLoginInfo().getMerId();
        QiniuConfigUploadFileVo qiniuConfigUploadFileVo = new QiniuConfigUploadFileVo();
        String fileUrl =  qiniuService.uploadFile(file, merId); //上传图片
        qiniuConfigUploadFileVo.setFileUrl(fileUrl);
        String fileTag = "[IMG]" + fileUrl + "[/IMG]";
        qiniuConfigUploadFileVo.setFileTag(fileTag);
        qiniuConfigUploadFileVo.setFilename(file.getOriginalFilename());
        return qiniuConfigUploadFileVo;
    }

    @ApiOperation(value = "上传本地视频", notes = "上传本地视频", response = QiniuConfigUploadFileVo.class)
    @ApiOperationSupport(order = 4)
    @PostMapping("/qiniu/uploadVideo")
    public Object uploadVideo(MultipartFile file){
        Long merId = getLoginInfo().getMerId();
        QiniuConfigUploadFileVo qiniuConfigUploadFileVo = new QiniuConfigUploadFileVo();
        String fileUrl =  qiniuService.uploadFile(file, merId); //上传图片
        qiniuConfigUploadFileVo.setFileUrl(fileUrl);
        String fileTag = "[VIDEO]" + fileUrl + "[/VIDEO]";
        qiniuConfigUploadFileVo.setFileTag(fileTag);
        qiniuConfigUploadFileVo.setFilename(file.getOriginalFilename());
        return qiniuConfigUploadFileVo;
    }

    @ApiOperation(value = "查询七牛云配置详情", notes = "查询七牛云配置详情", response = QiniuConfigInfoVo.class)
    @ApiOperationSupport(order = 5)
    @PostMapping("/qiniu/findQiniuConfigInfo")
    public Object findQiniuConfigInfo(){
        Long merId = getLoginInfo().getMerId();
        return qiniuService.findQiniuConfigInfo(merId);
    }
}
